package com.augurit.wf.jbpmex.task.multitask.proxy;

import java.util.List;

import org.jbpm.api.JbpmException;
import org.jbpm.api.TaskQuery;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;
import org.jbpm.pvm.internal.history.HistoryEvent;
import org.jbpm.pvm.internal.history.events.TaskActivityStart;
import org.jbpm.pvm.internal.history.model.HistoryTaskImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.session.DbSession;
import org.jbpm.pvm.internal.task.TaskDefinitionImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

import com.augurit.common.constant.OrgConstant;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.util.WfUtils;
import com.augurit.wf.jbpmex.task.multitask.completerule.base.ICompleteRule;

/**
 * 以会签计算结果作为跳转transition名称
 */
public class EqualModeProxy extends BaseModeProxy {

	public EqualModeProxy(TaskDefinitionImpl taskDefinition, ExecutionImpl execution){
		
		super(taskDefinition, execution);
	}
	
	/**
	 * 创建系统主任务
	 */
	public void createSuperTask() {
		DbSession dbSession = this.getBean(DbSession.class);

		superTask = dbSession.createTask();
		
		superTask.setName(execution.getActivityName() + "_" + WfConstant.TASK_ASSIGNEE_SYSTEM);
		superTask.setTaskDefinition(taskDefinition);
		superTask.setExecution(execution);
		superTask.setProcessInstance(execution.getProcessInstance());
		superTask.setSignalling(false);	//false，手工处理execution的跳转
		superTask.setAssignee(WfConstant.TASK_ASSIGNEE_SYSTEM);
		superTask.setIsMainTask(OrgConstant.ONE);
		superTask.setTaskType(WfConstant.TASK_TYPE_MULTITASK);
		superTask.setAssigneeName(WfConstant.TASK_ASSIGNEE_SYSTEM_NAME);
		
		//设置当前节点中文名称
		superTask.setActivityChineseName(execution.getActivity().getChineseName());
		    
		dbSession.save(superTask);
		
		//创建历史记录
		HistoryEvent.fire(new TaskActivityStart(superTask), execution);
	}
	
	/**
	 * 根据用户数创建子任务
	 */
	public void createSubTasks(){
		userKeys = this.getValidUserKeys();
		if(userKeys == null || userKeys.length == 0)
			throw new JbpmException("错误原因：用户数小于1！");

		for(int i=0; i<userKeys.length; i++) {
			this.createSubTask(superTask, i, userKeys[i]);
		}
		
		//去掉表量表的流程提示信息
		this.removeJmsVariable(superTask);
	}
	
	/**
	 * 创建子任务
	 */
	public void createSubTask(TaskImpl superTask, int index, String assignee){
		this.createSubTaskInBaseMode(superTask, index, assignee, 0);
	}
	
	
	/**
	 * 激活下一个或多个挂起的任务
	 * @param task
	 * @param executionImpl
	 * @param taskService
	 */
	public void enableNextSuspendedTask(TaskImpl curTask){
		
		TaskService taskService = this.getBean(TaskService.class);
		
		superTask = curTask.getSuperTask() != null ? curTask.getSuperTask() : curTask;
		List<Task> others = taskService.createTaskQuery().superTaskDbId(superTask.getDbid()).suspended().orderAsc(TaskQuery.PROPERTY_PRIORITY).list();
		
		if(others != null && others.size() > 0){
			TaskImpl subTask = (TaskImpl)others.get(0);
			subTask.setState(Task.STATE_OPEN);
			
			this.enableSuspendedHistoryTask(subTask);			
		}
	}
	
	public boolean isEnd(TaskImpl curTask) {
		
		TaskImpl superTask = curTask.getSuperTask() != null ? curTask.getSuperTask() : curTask;
		List<HistoryTaskImpl> hisSubTasks = superTask != null 
				? WfUtils.convertToHistoryTaskList(this.getSubTaskList(superTask.getDbid())) : null;

		ICompleteRule rule = multiTaskDefinition.getCompleteRule();
		rule.init(curTask, superTask, hisSubTasks);
		return rule.isEnd();
	}
	
	public boolean forceEnableNextSuspendedTask() {
		// TODO Auto-generated method stub
		return false;
	}
}
